package com.itheima.admin.controller;

import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.enums.HttpCodeEnum;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/channel")
@Api(value = "AdChannelController",tags = "频道管理")
public class AdChannelController {
    @Autowired
    private AdChannelService adChannelService;

    @PostMapping("/list")
    @ApiOperation("频道分页搜索")
    public PageResultVo<AdChannel> page(@RequestBody ChannelPageRequestDto dto){
        return adChannelService.pageList(dto);
    }

    @ApiOperation("新增频道")
    @PostMapping("/save")
    public ResultVo<AdChannel> save(@RequestBody AdChannel adChannel){
        if(adChannelService.saveChannel(adChannel)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/{id}")
    public ResultVo<AdChannel> findById(@PathVariable (name = "id") Long id){
        AdChannel adChannel = adChannelService.findById(id);
        return ResultVo.ok(adChannel);
    }

    @ApiOperation("更新频道")
    @PostMapping("/update")
    public ResultVo<AdChannel> updateById(@RequestBody AdChannel adChannel){
        //主键参数校验
        if(null == adChannel.getId()){
            return ResultVo.error(HttpCodeEnum.PARAM_REQUIRE);
        }

        boolean success = adChannelService.updateChannelById(adChannel);
        if (success){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @ApiOperation("通过id删除频道")
    @DeleteMapping("/del/{id}")
    public ResultVo<AdChannel> deleteById(@PathVariable(name = "id") Long id){
        if (adChannelService.deleteById(id)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }
}
